<?php

/**
 * This is the model class for table "{{cmp_cat}}".
 *公司自定义分类表
 * The followings are the available columns in table '{{cmp_cat}}':
 * @property integer $zpc_id
 * @property integer $zm_id
 * @property integer $zpc_type
 * @property integer $zpc_topid
 * @property integer $zpc_parentid
 * @property string $zpc_childid
 * @property string $zpc_catname
 * @property string $zpc_pic
 * @property string $zpc_seotitle
 * @property string $zpc_seokey
 * @property string $zpc_seodesc
 * @property integer $zpc_listorder
 * @property integer $inputtime
 * @property integer $updatetime
 */
class CmpCat extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return CmpCat the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{cmp_cat}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('zm_id, zpc_type, zpc_topid, zpc_parentid, zpc_listorder, inputtime, updatetime', 'numerical', 'integerOnly'=>true),
			array('zpc_catname, zpc_seotitle', 'length', 'max'=>100),
			array('zpc_pic, zpc_seokey, zpc_seodesc', 'length', 'max'=>255),
			array('zpc_childid', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('zpc_id, zm_id, zpc_type, zpc_topid, zpc_parentid, zpc_childid, zpc_catname, zpc_pic, zpc_seotitle, zpc_seokey, zpc_seodesc, zpc_listorder, inputtime, updatetime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(

		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'zpc_id' => 'id',
			'zm_id' => '会员ID',
			'zpc_type' => '类型',//1产品分类2新闻分类3相册
			'zpc_topid' => '顶级id',
			'zpc_parentid' => '父级id',
			'zpc_childid' => '子ID',
			'zpc_catname' => '分类名',
			'zpc_pic' => '分类图片',
			'zpc_seotitle' => 'SEO标题',
			'zpc_seokey' => 'SEO关键词',
			'zpc_seodesc' => 'SEO描述',
			'zpc_listorder' => '排序',
			'inputtime' => '创建时间',
			'updatetime' => '更新时间',
		);
	}
	
	/*
	 * 类型
	 * */
	public function getType($type=null){
		if($type==null){
			return array(
				'1'=>'产品分类',
				'2'=>'新闻分类',	
				'3'=>'相册',
				'4'=>'公司部门',
				'5'=>'公司角色',
			);
		}else{
			$level = $this->getType();
			if(array_key_exists($type,$level)){
				return $level[$type];
			}
		}
	}
	
	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('zpc_id',$this->zpc_id);
		$criteria->compare('zm_id',$this->zm_id);
		$criteria->compare('zpc_type',$this->zpc_type);
		$criteria->compare('zpc_topid',$this->zpc_topid);
		$criteria->compare('zpc_parentid',$this->zpc_parentid);
		$criteria->compare('zpc_childid',$this->zpc_childid,true);
		$criteria->compare('zpc_catname',$this->zpc_catname,true);
		$criteria->compare('zpc_pic',$this->zpc_pic,true);
		$criteria->compare('zpc_seotitle',$this->zpc_seotitle,true);
		$criteria->compare('zpc_seokey',$this->zpc_seokey,true);
		$criteria->compare('zpc_seodesc',$this->zpc_seodesc,true);
		$criteria->compare('zpc_listorder',$this->zpc_listorder);
		$criteria->compare('inputtime',$this->inputtime);
		$criteria->compare('updatetime',$this->updatetime);

		$criteria->order = " inputtime DESC ";//默认为时间倒序
		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
			'pagination'=>array(
					'pageSize'=>100,
			)
		));
	}
	/*
	 * 获取某个分类下面的子分类信息
	 * $catid 分类id
	 * $type 分类类别
	 *   */
	public function getCatlist($catid,$type){
			$criteria=new CDbCriteria;
			$criteria->compare('zpc_parentid',$catid);
			$criteria->compare('zpc_type',$type);
			$criteria->compare('zm_id',Company::model()->getCmpUserId());
			$criteria->order='zpc_listorder ASC';
			return new CActiveDataProvider($this, array(
					'criteria'=>$criteria,
			));
	}
	/*查询某个分类下面的子分类id
	 *   */
	public function getCatidarray($catid){
		$childid = self::model()->findByAttributes(array('zpc_id'=>$catid));
		if(isset($childid->zpc_childid)){
			return explode(',',$childid->zpc_childid);
		}else{
			return null;
		}
	}
	/*
	 * 获取当前会员自定产品分类
	 * @param type 1获取一级自定义分类，0全部自定义分类
	 * */
	public function getProductCat($type=0,$zmp_id=null){
		$zmp_id = empty($zmp_id)?Company::model()->getCmpUserId():(int)$zmp_id;
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',$zmp_id);
		$criteria->compare('zpc_type','1');
		$criteria->compare('zpc_parentid','0');
		$criteria->order='zpc_listorder ASC';
		$cat = $this->findAll($criteria);
		if(!empty($cat)){
			if($type==0){
				$cat = simplifyData($cat);
				foreach($cat as $key=>$val){
					$data[$key]['zpc_id']=$val['zpc_id'];
					$data[$key]['zpc_catname']=$val['zpc_catname'];
						$criteria=new CDbCriteria;
						$criteria->compare('zm_id',$zmp_id);
						$criteria->compare('zpc_type','1');
						$criteria->compare('zpc_parentid',$val['zpc_id']);
						$criteria->order='zpc_listorder ASC';
						$catchild = $this->findAll($criteria);
						if(!empty($catchild)){
							$catchild = simplifyData($catchild);
							$data[$key]['child'] =$catchild;
						}
				}
			}else{
				$data[] = '选择所有';
				foreach($cat as $key=>$val){
					$data[$val->zpc_id] =$val->zpc_catname;
				}
			}
		}
		return $data;
	}
	
	
	
	/*
	 * 获取 当前的商家 部门列表
	 * */
	public function getCmpAccount($type=null){
		if($type==null){
			$criteria=new CDbCriteria;
			$criteria->compare('zpc_type',4);
			$criteria->compare('zm_id',Company::model()->getCmpUserId());
			$section = $this->findAll($criteria);
			return CHtml::listData($section,'zpc_id','zpc_catname');
		}else{
			$level = $this->getCmpAccount();
			if(array_key_exists($type,$level)){
				return $level[$type];
			}
		}	
	}
	
	/*
	 * 获取 当前的商家 角色列表
	* */
	public function getCmpRole($type=null){
		if($type==null){
			$criteria=new CDbCriteria;
			$criteria->compare('zpc_type',5);
			$criteria->compare('zm_id',Company::model()->getCmpUserId());
			$section = $this->findAll($criteria);
			return CHtml::listData($section,'zpc_id','zpc_catname');
		}else{
			$level = $this->getCmpAccount();
			if(array_key_exists($type,$level)){
				return $level[$type];
			}
		}
	}
	
	/*
	 *  获取当前会员自定分类，用户分类下拉菜单
	 *  @param $type 类型：1产品分类，2新闻分类，3相册分类,自动添加“0”为未分类
	 *  @param $cmpid商家会员ID，默认为当前商家
	 */
	public function getCmpCat($msg='查看全部',$type=1,$cmpid=null){
		$cmpid = ($cmpid==null)?Company::model()->getCmpUserId():$cmpid;
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',$cmpid);
		$criteria->compare('zpc_type',$type);
		$criteria->order='zpc_listorder ASC';
		$cmpcat = $this->findAll($criteria);

		$data['']=$msg;
		$data['0']='未分类';
		foreach($cmpcat as $val){
			if(empty($val->zpc_childid))
					$data[$val->zpc_id]=$val->zpc_catname;
		}
		return $data;
	}
	

	/*
	 *  获取当前会员自定分类，用户分类下拉菜单
	*  @param $type 类型：1产品分类，2新闻分类，3相册分类,4商家部门,5角色，6链接
	*  @param $cmpid商家会员ID，默认为当前商家
	*/
	public function getCat($type=1,$cmpid=null){
		$cmpid = ($cmpid==null)?Company::model()->getCmpUserId():$cmpid;
		$criteria=new CDbCriteria;
		$criteria->compare('zm_id',$cmpid);
		$criteria->compare('zpc_type',$type);
		$criteria->order='zpc_listorder ASC';
		$cmpcat = $this->findAll($criteria);
		$data = CHtml::listData($cmpcat,'zpc_id','zpc_catname');
		return $data;
	}
	
	
}